/**
 * @Author:marina
 * @Date: 2025-02-14 10:35:24 
 * @Description:公共方法
 */


/**
 * 设置localStorage
 * @param key  键值名
 * @param value 值
 */
export function setSessionStorage(key: string, value: any): void {
    if (typeof window !== 'undefined') {
        sessionStorage.setItem(key, JSON.stringify(value));
    }
}

/**
 * 获取localStorage
 * @param key 键值名
 * @returns 
 */
export function getSessionStorage(key: string): any {
    if (typeof window !== 'undefined') {
        const item = sessionStorage.getItem(key);
        return item ? JSON.parse(item) : null;
    }
    return null;
}

/**
 * 清除特定键值名的localStorage
 * @param key 键值名
 */
export function removeSessionStorage(key: string): void {
    if (typeof window !== 'undefined') {
        sessionStorage.removeItem(key);
    }
}

/**
 * 清除所有sessionStorage
 */
export function clearAllSessionStorage(): void {
    if (typeof window !== 'undefined') {
        sessionStorage.clear();
    }
}
/**
 * 清除上下文
 * @param scene 场景
 * @param renderer 渲染器
 * @param animationID 
 */
export const destroy = (scene: any, renderer: any, animationID: number) => {
    try {
        scene.traverse((child: any) => {
            if (child.material) {
                child.material.dispose();
            }
            if (child.geometry) {
                child.geometry.dispose();
            }
            child = null;
        });
        scene.clear();
        renderer.dispose();
        renderer.forceContextLoss();
        renderer = null;
        // 去除animationFrame
        cancelAnimationFrame(animationID);
        // let gl = renderer.domElement.getContext("webgl");
        // gl && gl.getExtension("WEBGL_lose_context").loseContext();
    } catch (e) {
        console.log(e)
    }
}